package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyPersonRestrictDao;
import com.boss.salary.entity.AgencyPersonRestrict;
import com.boss.salary.service.IAgencyPersonRestrictService;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AgencyPersonRestrictServicelmpl implements IAgencyPersonRestrictService {
    @Autowired
    private IAgencyPersonRestrictDao agencyPersonRestrictDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAgencyPersonRestrict(AgencyPersonRestrict agencyPersonRestrict) {
        agencyPersonRestrict.setId(SysUtils.generateGuid());
        agencyPersonRestrict.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        agencyPersonRestrict.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        agencyPersonRestrict.setIsDeleted(SystemDictConstant.IS_FALSE);
        agencyPersonRestrictDao.insertAgencyPersonRestrict(agencyPersonRestrict);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAgencyPersonRestrict(String id, String mofDivCode) {
        agencyPersonRestrictDao.deleteAgencyPersonRestrict(id, mofDivCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAgencyPersonRestrict(AgencyPersonRestrict agencyPersonRestrict) {
        agencyPersonRestrict.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        agencyPersonRestrictDao.updateAgencyPersonRestrict(agencyPersonRestrict);
    }


    @Override
    public PageDTO<Map<String, Object>> queryAgencyPersonRestrictPage(PaginationDTO paginationDTO, String mofDivCode, List<String> childrenId, String state) {
        // 根据 单位 状态 查询  mofDivCode区划 childrenId单位 state状态
        List<Map<String, Object>> agencyPersonRestrictPage = agencyPersonRestrictDao.queryAgencyPersonRestrictPage(paginationDTO, mofDivCode, childrenId, state);
        // 根据 单位 状态 查询  mofDivCode区划 childrenId单位 state状态 查询条数
        int total = agencyPersonRestrictDao.queryAgencyPersonRestrict(mofDivCode, childrenId, state);
        PageDTO<Map<String, Object>> pageDTO = new PageDTO<>();
        pageDTO.setRows(agencyPersonRestrictPage);
        pageDTO.setTotal(total);
        return pageDTO;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUpdateAgencyPersonRestrict(List<AgencyPersonRestrict> agencyPersonRestrictList,UserDTO userDTO) {
        //通过agencyId删除
        agencyPersonRestrictDao.deleteAgencyPersonRestrict(agencyPersonRestrictList.get(0).getAgencyId(),userDTO.getProvince());
        //通过code查找当前工资类型
        List<AgencyPersonRestrict> meritPay = agencyPersonRestrictDao.selectWageType(userDTO);
        //addList新增
        List<AgencyPersonRestrict> addList = new ArrayList<>();
        //将放回的信息循环遍历存入
        for (AgencyPersonRestrict agencyPersonRestrict : agencyPersonRestrictList) {
            //将工资类型循环
            for(AgencyPersonRestrict WageType :meritPay ){
                //做匹配相同的code 存入工资id  工资name
                if(agencyPersonRestrict.getMeritPayCode().equals(WageType.getMeritPayCode())){
                    agencyPersonRestrict.setMeritPayId(WageType.getMeritPayId());
                    agencyPersonRestrict.setMeritPayName(WageType.getMeritPayName());
                }
            }
                agencyPersonRestrict.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                agencyPersonRestrict.setIsDeleted(SystemDictConstant.IS_FALSE);
                agencyPersonRestrict.setId(SysUtils.generateGuid());
                addList.add(agencyPersonRestrict);
        }
        //存入
        if(addList.size()>0){
            agencyPersonRestrictDao.insertAgencyPersonRestricts(addList);
        }
    }

    @Override
    public List<AgencyPersonRestrict> queryAgencyIdList(String agencyId, UserDTO userDTO) {
        List<AgencyPersonRestrict> list = agencyPersonRestrictDao.queryAgencyIdList(agencyId, userDTO);
        return list;
    }


    @Override
    public List<AgencyPersonRestrict> agencyType(String province) {
        List<AgencyPersonRestrict> list = agencyPersonRestrictDao.agencyType(province);
        return list;
    }

    @Override
    public List<AgencyPersonRestrict> currentUnitList(String agencyId, UserDTO userDTO) {
        List<AgencyPersonRestrict> list = agencyPersonRestrictDao.currentUnitList(agencyId, userDTO);
        return list;
    }

    @Override
    public Integer queryCurrentNumber(String agencyId, String payCode) {
        Integer currentNumber = agencyPersonRestrictDao.queryCurrentNumber(agencyId,payCode);
        if(currentNumber==null){
            currentNumber=0;
        }
        return currentNumber;
    }

}
